package com.zbkj.admin.controller.publicly;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyV3Response;
import com.zbkj.dal.annotation.CustomResponseAnnotation;
import com.zbkj.service.service.PayCallbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 支付回调
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/publicly/payment/callback")
@Api(tags = "支付回调控制器")
@CustomResponseAnnotation
public class PayCallbackController {

    @Autowired
    private PayCallbackService callbackService;

    @Deprecated
    @ApiOperation(value = "微信支付回调")
    @RequestMapping(value = "/wechat", method = RequestMethod.POST)
    public String weChat(@RequestBody String request) {
        log.info("微信支付回调 request ===> " + request);
        String response = callbackService.wechatPayCallback(request);
        log.info("微信支付回调 response ===> " + response);
        return response;
    }

    @Deprecated
    @ApiOperation(value = "支付宝支付回调 ")
    @RequestMapping(value = "/alipay", method = RequestMethod.POST)
    public String aliPay(HttpServletRequest request) {
        //支付宝支付回调
        log.info("支付宝支付回调 request ===> " + JSON.toJSONString(request.getParameterMap()));
        return callbackService.aliPayCallback(request);
    }

    /**
     * 微信退款回调
     */
    @Deprecated
    @ApiOperation(value = "微信退款回调")
    @RequestMapping(value = "/wechat/refund", method = RequestMethod.POST)
    public String weChatRefund(@RequestBody String request) {
        log.info("微信退款回调 request ===> " + request);
        String response = callbackService.weChatRefund(request);
        log.info("微信退款回调 response ===> " + response);
        return response;
    }

    @ApiOperation(value = "微信支付回调v3")
    @RequestMapping(value = "/wechat/v3", method = RequestMethod.POST)
    public String weChatV3(@RequestBody String request, HttpServletRequest servletRequest) {
        log.info("微信支付回调 request ===> " + request);
        String timestamp = servletRequest.getHeader("Wechatpay-Timestamp");
        String nonce = servletRequest.getHeader("Wechatpay-Nonce");
        String signature = servletRequest.getHeader("Wechatpay-Signature");
        String serialNo = servletRequest.getHeader("Wechatpay-Serial");
        log.info("微信支付回调 timestamp = {}, nonce = {}, signature = {}, serialNo={}", timestamp, nonce, signature, serialNo);

        //应对签名探测流量
        if (signature.startsWith("WECHATPAY/SIGNTEST/")) {
            return WxPayNotifyV3Response.fail("失败");
        }
        String response = callbackService.wechatPayCallback(request, timestamp, nonce, signature, serialNo);
        log.info("微信支付回调 response ===> " + response);
        return response;
    }

    /**
     * 微信退款回调V3
     */
    @ApiOperation(value = "微信退款回调v3")
    @RequestMapping(value = "/wechat/refund/v3", method = RequestMethod.POST)
    public String weChatRefund(@RequestBody String request, HttpServletRequest servletRequest) {
        log.info("微信退款回调 request ===> " + request);
        String timestamp = servletRequest.getHeader("Wechatpay-Timestamp");
        String nonce = servletRequest.getHeader("Wechatpay-Nonce");
        String signature = servletRequest.getHeader("Wechatpay-Signature");
        String serialNo = servletRequest.getHeader("Wechatpay-Serial");
        log.info("微信退款回调 timestamp = {}, nonce = {}, signature = {}, serialNo={}", timestamp, nonce, signature, serialNo);

        //应对签名探测流量
        if (signature.startsWith("WECHATPAY/SIGNTEST/")) {
            return WxPayNotifyV3Response.fail("失败");
        }
        String response = callbackService.weChatRefund(request, timestamp, nonce, signature, serialNo);
        log.info("微信退款回调 response ===> " + response);
        return response;
    }
}



